package net.entelijan.cobean.examples.showcase.text;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.text.DefaultTextInitializer;
import net.entelijan.cobean.text.converter.DateTextConverter;
import net.entelijan.cobean.text.converter.LongTextConverter;
import net.entelijan.cobean.text.validator.DateRangeValidator;
import net.entelijan.cobean.text.validator.NumberRangeValidator;
import net.entelijan.scf.IShowcase;

public class ValidatingTextShowcase implements
		IShowcase<SimpleMultiFailureFieldsTextPanel> {

	private ILitService service = null;
	private ICobean<String> nameCobean;
	private ICobean<Long> idCobean;
	private ICobean<Date> birthdayCobean;

	public ValidatingTextShowcase() {
		super();
	}

	@Override
	public void init(final SimpleMultiFailureFieldsTextPanel panel) {

		{
			DefaultTextInitializer<String> init = new DefaultTextInitializer<String>();
			init.setCoTextComponent(panel.getNameTextField());
			init.setCoFailureComponent(panel.getNameFailureTextField());
			init.setCoResetButton(panel.getResetButton());
			nameCobean = init.initialize();
		}
		{
			DefaultTextInitializer<Date> init = new DefaultTextInitializer<Date>();
			init.setCoTextComponent(panel.getBirthdayTextField());
			init.setCoFailureComponent(panel.getBirthdayFailureTextField());
			init.setCoResetButton(panel.getResetButton());
			init.setConverter(new DateTextConverter("dd.MM.yyyy"));
			init.setValidator(new DateRangeValidator<Date>("1.1.1900",
					"31.12.2050", "dd.MM.yyyy"));
			birthdayCobean = init.initialize();
		}
		{
			DefaultTextInitializer<Long> init = new DefaultTextInitializer<Long>();
			init.setCoTextComponent(panel.getIdTextField());
			init.setCoFailureComponent(panel.getIdFailureTextField());
			init.setCoResetButton(panel.getResetButton());
			init.setConverter(new LongTextConverter());
			init.setValidator(new NumberRangeValidator<Long>(0l, 100l));
			idCobean = init.initialize();
		}

		{
			// Set some initial data to the textfields.
			final LitAuthor author = service.loadAllAuthors().get(5);
			nameCobean.setValue(author.getName());
			idCobean.setValue(author.getId());
			birthdayCobean.setValue(author.getBirthday());
		}

		panel.getShowObjectButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, "name:'"
						+ nameCobean.getValue() + "' id:'"
						+ idCobean.getValue() + "'  birthday:'"
						+ birthdayCobean.getValue() + "'");
			}

		});
		panel.getUppercaseButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String val = nameCobean.getValue().toUpperCase();
				nameCobean.setValue(val);
			}

		});
		panel.getLowercaseButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String val = nameCobean.getValue().toLowerCase();
				nameCobean.setValue(val);
			}

		});
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

	public ICobean<String> getNameCobean() {
		return nameCobean;
	}

	public void setNameCobean(ICobean<String> nameCobean) {
		this.nameCobean = nameCobean;
	}

	public ICobean<Long> getIdCobean() {
		return idCobean;
	}

	public void setIdCobean(ICobean<Long> idCobean) {
		this.idCobean = idCobean;
	}

	public ICobean<Date> getBirthdayCobean() {
		return birthdayCobean;
	}

	public void setBirthdayCobean(ICobean<Date> birthdayCobean) {
		this.birthdayCobean = birthdayCobean;
	}

}
